package com.fingerth.crawler

import android.os.Environment
import androidx.multidex.MultiDexApplication
import androidx.room.Room
import com.fingerth.crawler.room.AppDatabase
import com.tencent.bugly.Bugly
import com.tencent.mmkv.MMKV
import java.io.File

class App : MultiDexApplication() {
    var roomNovel: AppDatabase? = null
    override fun onCreate() {
        super.onCreate()
        _app = this
        MMKV.initialize(this)
        //腾讯 bugly
        Bugly.init(applicationContext, "195818a52c", false)

        var dbPath = "room_novel.db"
        //val pFile: File? = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
//        if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) {
//            val sF: File? = File(Environment.getExternalStorageDirectory(), "51job/$dbPath")
//            sF?.let { dbPath = it.absolutePath }
//        } else {
            val file: File? = File(getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), dbPath)
            file?.let { dbPath = it.absolutePath }
//        }
        roomNovel = Room.databaseBuilder(applicationContext, AppDatabase::class.java, dbPath)
                .allowMainThreadQueries()
                //.addMigrations(MIGRATION_1_2, MIGRATION_2_3).build();//最正确的做法
                .fallbackToDestructiveMigration().build()//不推荐的做法
    }

    companion object {
        private var _app: App? = null
        val app: App get() = _app!!
    }

}